package com.highspeed.bdk.controller.logic.service;

import com.highspeed.bdk.TestMainApplication;
import com.highspeed.bdk.bean.entity.logic.App;
import com.highspeed.bdk.bean.entity.logic.LogicLink;
import com.highspeed.bdk.bean.entity.logic.LogicTopology;
import com.highspeed.bdk.service.logic.IAppService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestMainApplication.class})
@Transactional
public class LogicTopologyServiceTester extends BaseAppServiceTester {
    private LogicTopology logicTopology;

    @Before
    public void init() {
        LogicTopology pt = createLogicTopology();
        this.logicTopology = logicTopologyService.save(pt);

        logicTopologyService.flush();
        logicTopologyService.clear();
        for (int a = 0; a < 5; a++) {
            App app = createApp();
            app.setPreinstalledRedLogicTopology(this.logicTopology);
            appService.save(app);
            this.logicTopology.getPreinstalledApps().add(app);
        }
        logicTopologyService.flush();
        logicTopologyService.clear();


        App app = this.logicTopology.getPreinstalledApps().get(0);
        app.setLogicTopology(this.logicTopology);
        appService.save(app);
        this.logicTopology.getApps().add(app);
        logicTopologyService.flush();
        logicTopologyService.clear();

        app = this.logicTopology.getPreinstalledApps().get(1);
        app.setLogicTopology(this.logicTopology);
        appService.save(app);
        this.logicTopology.getApps().add(app);
        logicTopologyService.flush();
        logicTopologyService.clear();

        createLogicLink(logicTopology.getApps().get(0), logicTopology.getApps().get(1));

        logicTopologyService.flush();
        logicTopologyService.clear();

        this.logicTopology = logicTopologyService.findOne(this.logicTopology.getId());
        System.out.println(logicTopology.getApps().size());
    }


    @Test
    public void save() {
        Assert.assertNotNull(logicTopology);

        Map<String, Object> params1 = new HashMap<String, Object>();
        params1.put("logicTopology.id", logicTopology.getId());
        Page<App> apps = appService.query(params1, null);
        Assert.assertEquals(apps.getContent().size(), 2);

        LogicTopology lt = logicTopologyService.getOne(this.logicTopology.getId());
        Assert.assertEquals(lt.getPreinstalledApps().size(), 5);
    }

    @Test
    public void delete() {
        logicTopologyService.delete(logicTopology.getId());
        logicTopologyService.flush();
        logicTopologyService.clear();


        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id_eq", logicTopology.getId());
        params.put("enabled_eq", true);

        List logicTopology = logicTopologyService.queryToList(params, null);
        Assert.assertTrue(logicTopology.isEmpty());
    }


    @Test
    public void deleteApp() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("logicTopology.id", logicTopology.getId());
        Page<App> apps = appService.query(params, null);
        Assert.assertEquals(apps.getContent().size(), 2);

        appService.delete(logicTopology.getApps().get(1).getId());
        Page<App> nowApps = appService.query(params, null);
        Assert.assertEquals(nowApps.getContent().size(), 1);
    }

    @Test
    public void query() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", logicTopology.getId());
        List<LogicTopology> list = logicTopologyService.queryToList(params, null);
        Assert.assertEquals(list.size(), 1);
    }
}
